<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profile.photo"
            @click="$refs.iptFile.click()"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profile.name"
        @click="showNameDialog"
      />
      <!-- 修改用户名称的对话框 -->
      <van-dialog
        v-model="isShowNameDialog"
        title="修改名称"
        show-cancel-button
        :before-close="onNameDialogBeforeClose"
      >
        <!-- 输入框 -->
        <van-field
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-search
        />
      </van-dialog>
      <van-cell
        title="生日"
        is-link
        :value="profile.birthday"
        @click="showBirthPopup"
      />
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          :show-toolbar="true"
          @cancel="isShowBirth = false"
          @confirm="confirm"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api'
import Notify from '@/ui/Notify.js'
import moment from 'moment'
export default {
  name: 'UserEdit',
  data() {
    return {
      profile: {},
      isShowNameDialog: false,
      userName: '',
      isShowBirth: false, // 显示时间选择器
      minDate: new Date(1900, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期
    }
  },
  created() {
    this.getProfile()
  },
  methods: {
    async getProfile() {
      const res = await userProfileAPI()
      this.profile = res.data.data
    },
    // 文件选择方法
    async onFileChange(ev) {
      // 防止用户未选择图片
      if (ev.target.files.length === 0) return
      const fd = new FormData()
      fd.append('photo', ev.target.files[0])
      const res = await updatePhotoAPI(fd)
      this.profile.photo = res.data.data.photo
    },
    showNameDialog() {
      this.isShowNameDialog = true
      this.userName = this.profile.name
    },
    onNameDialogBeforeClose(action, done) {
      const regName = /^[a-zA-Z0-9_-]{2,5}$/

      if (action === 'confirm') {
        if (regName.test(this.userName)) {
          done()
        } else {
          Notify({ type: 'warning', message: '用户名中英文和数字2-5位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        done()
      }
    },
    showBirthPopup() {
      this.isShowBirth = true
      this.currentDate = new Date(this.profile.birthday) // 设置data组件默认显示时间
    },
    async confirm() {
      const date = moment(this.currentDate).format('YYYY-MM-DD')
      await updateProfileAPI({
        birthday: date
      })
      // 前端页面同步
      this.profile.birthday = date
      // 时间选择器关闭
      this.isShowBirth = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
